import Vue from "vue";
import VueRouter from "vue-router";
import store from '../store/index';

/* 引入 NProgress 组件 */
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

Vue.use(VueRouter);

const router = new VueRouter({
    base: process.env.BASE_URL
});

// 添加路由规则
import errorRouter from './modules/error';
import systemRouter from './modules/system';
import upmsRoute from './modules/upms';

router.addRoutes([
    ...errorRouter,
    ...systemRouter,
    ...upmsRoute,
]);

//  全局前置守卫
router.beforeEach((to,form,next) =>{
    NProgress.start();
    //  判断资源是否需要登录可以访问
    let permission = to.meta.permission;
    if (undefined !== permission){
        //  判断是否含有 AccessToken
        let accessToken = store.getters.getAccessToken;
        if (!accessToken) {
            //  跳转到登录页面
            let query = to.query;
            query['callback'] = to.fullPath;
            next({ name : 'login',query : query });
            NProgress.done();
            return;
        }
        //  权限校验

    }
    next();
});

//  全局后置钩子
router.afterEach(() => {
    NProgress.done();
});

export default router;
